package com.fenye.testflow.controller;

import com.fenye.testflow.model.common.Result;
import com.fenye.testflow.model.common.ResultCodeEnum;
import com.fenye.testflow.model.dto.LoginDto;
import com.fenye.testflow.model.entity.system.SysUser;
import com.fenye.testflow.model.vo.LoginVo;
import com.fenye.testflow.model.vo.ValidateCodeVo;
import com.fenye.testflow.service.SysUserService;
import com.fenye.testflow.service.ValidateService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController()
public class IndexController {
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private ValidateService validateService;
    @PostMapping("/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto){

        LoginVo loginVo = sysUserService.login(loginDto);
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }




    @PostMapping("/register")
    public Result register(@RequestBody LoginDto loginDto){
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(loginDto, sysUser);
        sysUserService.save(sysUser);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode(){
        ValidateCodeVo validateCodeVo = validateService.generateValidateCode();
        return Result.build(validateCodeVo, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/getUserInfo")
    public Result<SysUser> getUserInfo (@RequestHeader(name = "token") String token ){
        SysUser userInfo = sysUserService.getUserInfo(token);
        return Result.build(userInfo, ResultCodeEnum.SUCCESS);
    }

}
